package com.mqtt.ui.base;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.widget.TextView;

import com.mqtt.R;
import com.mqtt.dao.MQTTBean;
import com.mqtt.dao.UpdateMqttConnection;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.litepal.LitePal;

import java.util.List;

import butterknife.ButterKnife;

/**
 * @Author xiaowen
 * @Email xiaowen.wxf@google.com
 * @Date 2018/10/19 .
 * @Description This is BaseActivity.
 */
public abstract class BaseActivity extends AppCompatActivity {

    protected Toolbar mToolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayoutId());
        ButterKnife.bind(this);
        if (!EventBus.getDefault().isRegistered(this))
            EventBus.getDefault().register(this);
        initTitle();
        initData();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        try {
            EventBus.getDefault().unregister(this);
        } catch (Exception e) {
        }
    }

    public abstract void initData();

    public abstract int getLayoutId();

    public String getTitleName(){
        return getString(R.string.app_name);
    }

    /**
     * 是否显示返回按钮，子类可复写
     *
     * @return
     */
    public boolean showHomeBackMenu() {
        return true;
    }



    /**
     * 初始化标题
     */
    private void initTitle() {
        try {
            mToolbar = (Toolbar) findViewById(R.id.toolbar_base);
            if (mToolbar == null ) {
                return;
            }
            mToolbar.setTitle(getTitleName());
            setSupportActionBar(mToolbar);
            getSupportActionBar().setDisplayHomeAsUpEnabled(showHomeBackMenu());
            getSupportActionBar().setDisplayShowHomeEnabled(showHomeBackMenu());
        } catch (Exception e) {
        }
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == android.R.id.home) {
            if(isFinish()){
                finish();
            }
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    public boolean isFinish() {
        return true;
    }


    public String getText(TextView tv){
        return tv.getText().toString().trim();
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void upDateSubscribe(String str){
    }


    public void startActivity(Class<?> openClass, Bundle bundle) {
        startAct(openClass,0,bundle);
    }

    public void openActivityForResult(Class<?> openClass, int requestCode, Bundle bundle) {
        startAct(openClass,requestCode,bundle);
    }

    private void startAct(Class<?> openClass, int requestCode, Bundle bundle){
        Intent intent = new Intent(this,openClass);
        if(null != bundle){
            intent.putExtras(bundle);
        }
        startActivityForResult(intent,requestCode);
    }


}